<?php
/**
 * 管理员角色
 */

namespace App\Http\Controllers\Admin;

use App\Http\Requests\RoleRequest;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class RoleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $result = $this->_repoService->index($request);
        return $this->display('role.index', $result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return $this->display('role.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(RoleRequest $request)
    {
        //
        if ($request->ajax()) {
            $result = $this->_repoService->store($request->all());
            if ($result['code'] == 1) {
                return $this->success('新增成功');
            } else {
                return $this->error($result['message']);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = Role::find($id);
        $this->assign('vo', $role);
        return $this->display('role.edit');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(RoleRequest $request, $id)
    {
        //
        if ($request->ajax()) {
            $result = $this->_repoService->update(['id' => $id], $request->all());
            if ($result['code'] == 1) {
                return $this->success('更新成功');
            } else {
                return $this->error($result['message']);
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        if ($request->ajax()) {
            $result = $this->_repoService->destroy(['id' => $id]);
            if ($result['code'] == 1) {
                return $this->success('删除成功');
            } else {
                return $this->error($result['message']);
            }
        }
    }

    /**
     * @param Request $request
     * @param $id
     */
    public function permission(Request $request, $id)
    {
        $role = Role::find($id);
        if ($request->ajax()) { //保存授权信息
            if (is_null($role)) {
                return $this->error('查询角色信息失败');
            }
            try{
                $rights = $request->input('rights');

                $permissions = Permission::findMany($rights);

                $mePermissions = $role->permissions;
                //要增加的权限
                $addPermissions = $permissions->diff($mePermissions);

                foreach ($addPermissions as $permission) {
                    $role->grantPermission($permission);
                }
                //要删除的权限
                $delPermissions = $mePermissions->diff($permissions);
                foreach ($delPermissions as $permission) {
                    $role->deletePermission($permission);
                }
            }catch (\Exception $e){
                dd($e);
                return $this->error($e->getMessage());
            }

            return $this->success('授权成功', ['redirect' => route('role.index')]);
        } else {
            //获取角色权限
            $perminssions = DB::table('permission_role')->where('role_id', $id)->pluck('permission_id')->toArray();
            //获取所有菜单信息
            $menus = Permission::submenus(0, false, true);
            return $this->display('role.permission', ['menus' => $menus, 'role' => $role, 'id' => $id, 'permissions' => $perminssions]);
        }
    }
}
